package cn.dinkevin.xui.widget.surface;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import cn.dinkevin.xui.util.LogUtil;

/**
 * Created by ChengPengFei on 2017/1/4 0004.
 */

public class VideoSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private int width;
    private int height;

    private SurfaceHolder holder;
    private boolean isSurfaceViewAvailable = false; // 当前 SurfaceView 是否可用

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (width != 0 && height != 0) {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            setMeasuredDimension(MeasureSpec.makeMeasureSpec(width, widthMode), MeasureSpec.makeMeasureSpec(height, heightMode));
        }
    }

    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
        requestLayout();
    }


    /**
     * SurfaceView 监听
     */
    public interface SurfaceViewListener {

        /**
         * 当 SurfaceView 可用回调
         *
         * @param holder
         */
        void onSurfaceCreated(SurfaceHolder holder);

        /**
         * 当 SurfaceView 销毁时回调
         *
         * @param holder
         */
        void onSurfaceDestroyed(SurfaceHolder holder);
    }

    private SurfaceViewListener listener;

    /**
     * 绑定 SurfaceView 事件监听回调
     *
     * @param listener SurfaceViewListener
     */
    public void setOnSurfaceViewListener(SurfaceViewListener listener) {
        this.listener = listener;
    }

    public VideoSurfaceView(Context context) {
        this(context, null);
    }

    public VideoSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (attrs == null)
            return;

        holder = getHolder();
        holder.setFormat(PixelFormat.TRANSPARENT);                  //translucent半透明 transparent透明
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.addCallback(this);

        Rect rect = holder.getSurfaceFrame();
        LogUtil.d("videosurface", rect.left, rect.right, rect.top, rect.bottom);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isSurfaceViewAvailable = true;
        if (this.listener != null)
            this.listener.onSurfaceCreated(holder);
        LogUtil.d("surfaceCreated");
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        LogUtil.d("surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isSurfaceViewAvailable = false;
        if (listener != null)
            listener.onSurfaceDestroyed(holder);
        LogUtil.d("surfaceDestroyed");
    }

    /**
     * 获取当前 SurfaceView 是否可用
     *
     * @return
     */
    public boolean isSurfaceViewAvailable() {
        return isSurfaceViewAvailable;
    }
}
